package com.ricky.job;

import java.util.Map;

import org.apache.log4j.Logger;

import com.ricky.core.net.abs.Request;
import com.ricky.core.net.msg.Context;
import com.ricky.handler.Default;
import com.ricky.handler.HandlerFactory;
import com.ricky.handler.IHandler;

@SuppressWarnings("rawtypes")
public class MsgTask implements Runnable {

	protected static Logger log = Logger.getLogger(MsgTask.class);

	private Context<Map<String,Object>> context;
	private Request request;

	public MsgTask(Request request,Context<Map<String,Object>> context) {
		this.context=context;
		this.request=request;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void run() {
		String handName=context.getHead();
		String handKey=HandlerFactory.Action_prefix+"."+handName;
		if(request.isValidate())
		{
			IHandler handler=request.getHandler(handKey);
			if(handler==null)
			{
				handler=HandlerFactory.createHandler(HandlerFactory.Package_Action,handName);
				if(handler==null)
					handler=request.getHandler(Default.defaultHandName);
				request.register(handKey, handler);
			}
			
			if(handler!=null)
				handler.proccess(request,context.getContent());
		}
	}

}
